package com.bing;

import com.bing.server.ChatServer;
import com.bing.client.ChatClient;

import java.util.Scanner;

/**
 * 聊天室主程序
 */
public class App {
    public static void main(String[] args) {
        System.out.println("==== 网络聊天室 ====");
        System.out.println("请选择启动模式:");
        System.out.println("1. 服务器模式");
        System.out.println("2. 客户端模式");
        System.out.print("请输入选择 (1/2): ");
        
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.nextLine();
        
        try {
            if ("1".equals(choice)) {
                // 服务器模式
                System.out.print("请输入服务器端口 (默认8888): ");
                String portStr = scanner.nextLine();
                int port = portStr.isEmpty() ? 8888 : Integer.parseInt(portStr);
                
                System.out.println("启动服务器，端口: " + port);
                new ChatServer(port).start();
            } else if ("2".equals(choice)) {
                // 客户端模式
                System.out.print("请输入服务器地址 (默认127.0.0.1): ");
                String host = scanner.nextLine();
                host = host.isEmpty() ? "127.0.0.1" : host;
                
                System.out.print("请输入服务器端口 (默认8888): ");
                String portStr = scanner.nextLine();
                int port = portStr.isEmpty() ? 8888 : Integer.parseInt(portStr);
                
                System.out.println("连接服务器: " + host + ":" + port);
                new ChatClient(host, port).start();
            } else {
                System.out.println("无效选择，程序退出");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
